﻿using System;
using System.Collections.Generic;
using System.Text;
using Model.Task;
using IBLL;
using Quartz;
using System.Data;
using BLL.Business;
using Common.Logging;
using Utility;
using BLL.Action.Core.Base;
using IBLL.Business;

namespace BLL.Action.Job
{
    public class JobDefault:JobBase
    {
        public const string Hotelid = "hotelid";
        private const string DefaultAction = "BLL.Action.ActionDefault,BLL.Action";
        public delegate void ExecuteAction(object define);
        ILog log = LogManager.GetLogger(LogDefault.AppDebug);
        public override void Execute(JobExecutionContext context)
        {
            try
            {
                log.Debug(string.Format("job:{0}", context.JobDetail.Name));

                base.Execute(context);

                string tasklistId = context.JobDetail.Name;

                DataSet ds = TaskBusinessHolder.GetTask(tasklistId);

                List<TaskDefine> tasks = new List<TaskDefine>();
                GetTaskdefine(ds, tasks);
                log.Debug(string.Format("taskcount:{0},{1}",context.JobDetail.Name, tasks.Count));
                foreach (TaskDefine define in tasks)
                {
                    log.Debug(string.Format("begin start action:{0}", define.Interface));
                    if (StringHelper.IsEmty(define.Action))
                        define.Action = DefaultAction;
                    IAction action = Factory.CreateAction(define.Action);
                    ExecuteAction delegateAction = new ExecuteAction(action.Execute);//;
                    delegateAction.BeginInvoke(define, ExecuteActionCallBack, delegateAction);
                   
                }
            }
            catch (Exception ex)
            {
                log.Debug(ex);
            }
        }
        public void ExecuteActionCallBack(IAsyncResult ar)
        {
            ExecuteAction delegateAction = ar.AsyncState as ExecuteAction;
            delegateAction.EndInvoke(ar);
        }


        public override ITaskBusiness TaskBusinessHolder
        {
            get
            {
                TaskBusiness task = new TaskBusiness();
                ContextAwareHolder.AutowireObject(task);
                return task;
            }
             
        }
    }
}
